package com.hihonor.sound;

import cn.hutool.core.io.resource.ResourceUtil;
import com.hihonor.Main;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.BufferedInputStream;

/**
 * @author 李志雄
 * @since 2023-10-14
 */
public class SoundUtil {
    private SoundUtil() {
    }

    public static void playSuccess(Main main) {
        playAudio(main, "success.wav");
    }

    public static void playFail(Main main) {
        playAudio(main, "fail.wav");
    }

    private static void playAudio(Main main, String fileName) {
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new BufferedInputStream(ResourceUtil.getStream(fileName))));
            clip.start();
        } catch (Exception e) {
            main.showFailMsg("语音播放失败," + e.getMessage());
        }
    }
}
